<template>
  <div class="pagination">
    <button @click="sendPageNo(pageNo-1)" :disabled="pageNo===1">上一页</button>
    <button v-show="startAndEnd.start>1" @click="$emit('change-pageNo',1)">1</button>
    <span v-show="startAndEnd.start>2">...</span>
    <button :class="{active:startAndEnd.start + index===pageNo}" v-for="(_,index) in (startAndEnd.end-startAndEnd.start + 1)" :key="_" @click="sendPageNo(startAndEnd.start + index)">{{startAndEnd.start + index}}</button>
    <button v-show="startAndEnd.end<totalPage-1">···</button>
    <button v-show="startAndEnd.end<totalPage" @click="$emit('change-pageNo',totalPage)">{{totalPage}}</button>
    <!-- <button @click="sendPageNo(pageNo+1)" :disabled="pageNo===totalPage">下一页</button> -->
    <button @click="$emit('change-pageNo',pageNo+1)" :disabled="pageNo===totalPage">下一页</button>
    <span>共 {{total}} 条</span>
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: ['total', 'pageSize', 'continues', 'pageNo'],
  computed: {
    // 1. 计算总页码
    totalPage() {
      const { total, pageSize } = this;
      // this.total/this.pageSize
      return Math.ceil(total / pageSize);
    },
    // 2. 计算连续页码的开始页和结束页
    startAndEnd() {
      // 2.1 先解构出连续页码 和当前页码
      const { pageNo, continues, totalPage } = this;
      // 2.2 根据当前页码计算前面的和后面的对称页码   14  15  16 17  18
      let start, end;
      // 2.4 特殊情况一 如果连续页码大于了总页码 则起始页就是1 结束页就是totalPage
      //       比如，总数量是91条数量，但是一页要显示30条，则只需要4页即可
      // 但是连贯页码却是5，当前页码是3，应该如何？ 1234
      if (continues > totalPage) {
        start = 1;
        end = totalPage;
      } else {
        start = pageNo - parseInt(continues / 2);
        end = pageNo + Math.trunc(continues / 2);
        // 2.5 特殊情况二 当起始页小于1的时候 则起始页只能等于1 结束页就是continues
        if (start < 1) {
          start = 1;
          end = continues;
        }

        // 2.6 特殊情况三 当结束页超过了总页码  则结束页只能是总页码
        if (end > totalPage) {
          //25 26 27 28 29 30 31
          end = totalPage;
          start = totalPage - continues + 1;
        }
      }

      // 2.3 返回这两个值
      return { start, end };
    }
  },
  methods: {
    // 1. 更改页码 将当前被单击的页码值发送给search组件
    sendPageNo(currentPage) {
      this.$emit('change-pageNo',currentPage)
    }
  }
};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #c81623;
      color: #fff;
    }
  }
  span {
    display: inline-block;
    line-height: 28px;
    font-size: 14px;
    color: gray;
    vertical-align: middle;
  }
}
</style>